package com.infore.airsupervise.biz.impl;

import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.amap.api.maps.model.LatLng;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.app.MyApplication;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.biz.UserLocationBiz;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.HttpAPI;
import com.infore.airsupervise.http.HttpCallBack;
import com.infore.airsupervise.http.HttpUtil;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JSONUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * @features:
 * @author: WuTao
 * @date: 2017/4/20 08:31
 */
public class UserLocationBizImpl implements UserLocationBiz {
    @Override
    public Call<BaseResponse> commitUserLocationRequest(String userId, double lat, double lng, String address, final OnServerResponseListener<BaseResponse> listener) {
        Map<String , Object> params = new HashMap<>();
        params.put(Constants.USER_ID_KEY,userId);
        params.put("lat" , lat);
        params.put("lnt", lng);
        params.put("address", address);
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.COMMIT_LOCATION_USEAR,params);
        call.enqueue( new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                listener.onSucceed(response, MsgConst.OPERATION_SUCCESS);
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.OPERATION_SUCCESS,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title,desc);
            }
        });
        return call;
    }

    @Override
    public Call<BaseResponse> getPsonLocationRequest(String userId, final OnServerResponseListener<List<UserE>> listener) {
        Map<String , Object> params = new HashMap<>();
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_PSON_LOCATION,params);
        call.enqueue( new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<UserE> userEList = JSONUtil.parseList(response.getData(),Constants.RESPONSE_BEAN_LIST_KEY,UserE.class);
                listener.onSucceed(userEList,MsgConst.GET_DATA_SUCCESS);

            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return null;
    }

    @Override
    public void saveCurrentLatLng(LatLng latLng) {
        SharedPreferences sp=  MyApplication.getInstance().getSharedPreferences(Constants.SP_FILE_USER, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor =sp.edit();
        String str_latLng= JSON.toJSONString(latLng, false);
        editor.putString(Constants.SP_LAST_LOCATION_KEY, str_latLng);
        editor.commit();
    }

    @Override
    public LatLng getLastLatLng() {
        SharedPreferences sp=  MyApplication.getInstance().getSharedPreferences(Constants.SP_FILE_USER, Context.MODE_PRIVATE);
        String str_latLng = sp.getString(Constants.SP_LAST_LOCATION_KEY, "");

        LatLng latLng = null;

        if (!"".equals(str_latLng)) {
            JSONObject jo = JSON.parseObject(str_latLng);
            double latitude = jo.getDouble("latitude");
            double longitude = jo.getDouble("longitude");
            latLng = new LatLng(latitude, longitude);
        }

        return latLng;
    }
}
